package com.design_mode;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * Created by dustaple on 8/4/14.
 */
public class FlyWeightPattern {
    public static void main(String[] args) {
        ICookFood buyFoodMaterial = new BuyFoodMaterial();

        buyFoodMaterial.addFoodMaterial(new Beef());
        buyFoodMaterial.addFoodMaterial(new Flour());
        buyFoodMaterial.addFoodMaterial(new Rice());

        CookRiceWithBeef cookRiceWithBeef = new CookRiceWithBeef();
        cookRiceWithBeef.cook();
        CookNoodleWithBeef cookNoodleWithBeef = new CookNoodleWithBeef();
        cookNoodleWithBeef.cook();
    }
}

abstract class IFoodMaterial {
    protected String name;

    public String getName() {
        return name;
    }

    abstract void what();
}

class Flour extends IFoodMaterial {
    Flour() {
        super.name = "面粉";
    }

    @Override
    public void what() {
        System.out.println("我是面粉.");
    }
}

class Beef extends IFoodMaterial {
    Beef() {
        super.name = "牛肉";
    }

    @Override
    public void what() {
        System.out.println("我是牛肉.");
    }
}

class Rice extends IFoodMaterial {
    Rice() {
        super.name = "大米";
    }

    @Override
    public void what() {
        System.out.println("我是大米.");
    }
}

abstract class ICookFood {
    static Vector<IFoodMaterial> foodMaterials = new Vector<IFoodMaterial>();

    public void addFoodMaterial(IFoodMaterial foodMaterial) {
        foodMaterials.add(foodMaterial);
    }

    public abstract void cook();
}

class BuyFoodMaterial extends ICookFood {
    @Override
    public void cook() {
        System.out.println("我只买食材，不做饭...");
    }
}

class CookRiceWithBeef extends ICookFood {
    @Override
    public void cook() {
        Iterator<IFoodMaterial> iFoodMaterialIterator = foodMaterials.iterator();
        while (iFoodMaterialIterator.hasNext()) {
            IFoodMaterial foodMaterial = iFoodMaterialIterator.next();
            if ("牛肉".equals(foodMaterial.getName())) {
                foodMaterial.what();
                System.out.println("红烧...");
                //iFoodMaterialIterator.remove();
                break;
            }
        }
        iFoodMaterialIterator = foodMaterials.iterator();
        while (iFoodMaterialIterator.hasNext()) {
            IFoodMaterial foodMaterial = iFoodMaterialIterator.next();
            if ("大米".equals(foodMaterial.getName())) {
                foodMaterial.what();
                System.out.println("蒸米饭...");
                iFoodMaterialIterator.remove();
                break;
            }
        }
        System.out.println("将牛肉盖到米饭上...");
        System.out.println("牛肉米饭成功");
    }
}

class CookNoodleWithBeef extends ICookFood {
    @Override
    public void cook() {
        Iterator<IFoodMaterial> iFoodMaterialIterator = foodMaterials.iterator();
        while (iFoodMaterialIterator.hasNext()) {
            IFoodMaterial foodMaterial = iFoodMaterialIterator.next();
            if ("牛肉".equals(foodMaterial.getName())) {
                foodMaterial.what();
                System.out.println("红烧...");
                System.out.println("牛肉用没了");
                iFoodMaterialIterator.remove();
                break;
            }
        }
        iFoodMaterialIterator = foodMaterials.iterator();
        while (iFoodMaterialIterator.hasNext()) {
            IFoodMaterial foodMaterial = iFoodMaterialIterator.next();
            if ("面粉".equals(foodMaterial.getName())) {
                foodMaterial.what();
                System.out.println("和面...擀面...做面条...煮面...");
                break;
            }
        }
        System.out.println("将牛肉放到面里...");
        System.out.println("牛肉面成功");
    }
}
